package com.wjyf.reducing_bento.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjyf.reducing_bento.annotation.AuthCheck;
import com.wjyf.reducing_bento.common.BaseResponse;
import com.wjyf.reducing_bento.common.ErrorCode;
import com.wjyf.reducing_bento.common.ResultUtils;
import com.wjyf.reducing_bento.model.dto.CouponAddDto;
import com.wjyf.reducing_bento.model.entity.CouponBean;
import com.wjyf.reducing_bento.service.CouponService;
import com.wjyf.reducing_bento.model.vo.CouponVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.wjyf.reducing_bento.constant.UserConstant.ADMIN;

/**
 * <p>
 * 优惠券基础配置表 前端控制器
 * </p>
 *
 * @author wjyf
 * @since 2023-03-14 08:36:32
 */
@RestController
@RequestMapping("/coupon-bean")
@Api(tags = "优惠券相关解决")
public class CouponController {

    @Resource
    private CouponService couponService;

    @PostMapping("add")
    @ApiOperation("新增优惠卷")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse add(@RequestBody CouponAddDto couponAddDto) {
        boolean isSuccess = couponService.add(couponAddDto);
        return ResultUtils.success(isSuccess);
    }

    @DeleteMapping
    @ApiOperation("删除优惠卷")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse delete(@RequestParam @ApiParam("优惠卷id") Integer codId) {
        if (codId == null || codId <= 0) return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        boolean removeById = couponService.removeById(codId);
        return removeById ? ResultUtils.success(removeById) : ResultUtils.error(ErrorCode.OPERATION_ERROR);
    }

    @GetMapping("/get")
    @ApiOperation("获取优惠卷分页")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse<Page<CouponBean>> get( @RequestParam @ApiParam("当前页号") Long current,
                             @RequestParam @ApiParam("页面大小") Long pageSize) {
        Page<CouponBean> page = couponService.get(current, pageSize);
        return ResultUtils.success(page);
    }

    @GetMapping("/getOneself")
    @ApiOperation("用户获取自己的优惠卷")
    public BaseResponse getOneself(HttpServletRequest request){
        List<CouponVo> couponVoList =  couponService.getOneself(request);
        return ResultUtils.success(couponVoList);
    }

}
